<?php
namespace Home\Controller;


class DealdobidController extends  BaseController
{
    public function index(){

        $root = array();

        $user =  \es_session::get('user_info');
        $root['session_id'] = \es_session::id();
        $user_id  = intval($user['id']);
        if ($user_id >0){
            $root['user_login_status'] = 1;

            $rs_paypassword_array = M('user')->where('id='.$user_id)->find();
            if($rs_paypassword_array){
                $GLOBALS['user_info']['paypassword'] = $rs_paypassword_array['paypassword'] ;
            }
            $id = intval($_REQUEST['id']);

            $deal = get_deal($id);
            $bid_money = floatval($_REQUEST["bid_money"]);
            $buy_number = $_REQUEST["buy_number"];
            if($deal['uloadtype'] == 1 && $buy_number > 1){
                $bid_money = $buy_number * $bid_money;
            }

            $bid_paypassword = strim($_REQUEST['bid_paypassword']);
            $ecv_id = intval($_REQUEST['ecv_id']);
            if( $ecv_id > 0 ){
                //判断红包使用情况
                $ecv_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."ecv where id = ".intval($ecv_id)." AND (begin_time =0 OR (begin_time > 0 AND begin_time < ".TIME_UTC.")) AND (end_time = 0 OR (end_time > 0  AND (end_time +24*3600 - 1) > ".TIME_UTC.")) AND (use_limit =0  OR (use_limit >0 AND use_limit - use_count > 0)) AND user_id=".$user_id);
                //echo json_encode($ecv_info);
                if(empty($ecv_info) || empty($ecv_info['money'])){
                    $root['status'] = 1;
                    $root['show_err'] = '该红包不在使用状态，请重新选择！';
                    output($root);
                }
            }
            if(empty($GLOBALS['user_info']['paypassword'])){
                $root['status'] = 1;
                $root['show_err'] = '请设置支付密码！';
                output($root);
            }
            //$bid_money == 0 || $bid_money <0
            if( $bid_money == 0 || $bid_money <0 ){
                $root['status'] = 1;
                $root['show_err'] = '请输入金额';
                output($root);
            }
            if(empty($bid_paypassword) ){
                $root['status'] = 1;
                $root['show_err'] = '请输入支付密码！';
                output($root);
            }else{
                if(md5($bid_paypassword) != $GLOBALS['user_info']['paypassword']){
                    $root["show_err"] = '请输入正确支付密码！';
                    $root["error_code"] = '请输入正确支付密码！';
                    $root['status'] = 1;
                    output($root);
                }
            }
            $interestrate_id = intval($_REQUEST['interestrate_id']);
            $use_interestrate = intval($_REQUEST['use_interestrate']);

            $status = dobid2($id,$bid_money,$bid_paypassword,0,$ecv_id,0,$interestrate_id,$use_interestrate);

            $root['status'] = $status['status'];
            if($status['status'] == 2){
                $root['response_code'] = 1;
                $root['app_url'] = $status['jump'] ;//str_replace("&from=index","&from=wap",$status['jump']);
                $class_name = getCollName();
                if(strtolower($class_name)=="fuioupay"){
                    $root['ajax_deal'] = 1;
                }
            }else if($status['status'] != 1){
                $root['response_code'] = 0;
                $root['show_err'] = $status['show_err'];
            }else{
                $root['response_code'] = 1;
                $root['show_err'] = 'deal_bid_success';//$GLOBALS['lang']['DEAL_BID_SUCCESS'];
                $root['id'] = $id;
            }
            $root['test'] = $buy_number;
            $root['test2'] = $status['show_err'];
        }else{
            $root['response_code'] = 0;
            $root['show_err'] ="未登录";
            $root['user_login_status'] = 0;
        }
        $root['error_code'] = $status['error_code'] ? $status['error_code'] : 0;
        output($root);
    }
}
?>
